package fi.jamk.ourcookingapp.backend.architecture.services;

import java.util.List;
import java.util.concurrent.ExecutionException;

import org.json.JSONException;

import fi.jamk.ourcookingapp.backend.MyTools;
import fi.jamk.ourcookingapp.backend.architecture.interfaces.ICommentManager;
import fi.jamk.ourcookingapp.backend.architecture.interfaces.IUserManager;
import fi.jamk.ourcookingapp.backend.architecture.manager.CommentManager;
import fi.jamk.ourcookingapp.backend.architecture.manager.UserManager;
import fi.jamk.ourcookingapp.backend.exceptions.EmptyInputException;
import fi.jamk.ourcookingapp.backend.exceptions.NoUserException;
import fi.jamk.ourcookingapp.backend.exceptions.WebServiceException;
import fi.jamk.ourcookingapp.backend.model.Comment;
import fi.jamk.ourcookingapp.backend.model.Receipt;
import fi.jamk.ourcookingapp.backend.model.User;


public class UserServices implements IUserManager, ICommentManager {

	private IUserManager uManager;
	
	private ICommentManager cManager;
	
	public UserServices() {
		this.uManager = new UserManager();
		this.cManager = new CommentManager();
	}

	public boolean createComment(Comment comment) throws InterruptedException, ExecutionException, JSONException {
		return this.cManager.createComment(comment);
	}

	public List<Comment> commentsByReceipt(Receipt receipt) throws InterruptedException, ExecutionException, JSONException {
		return this.cManager.commentsByReceipt(receipt);
	}

	public User createUser(User user) throws EmptyInputException, InterruptedException, ExecutionException, JSONException, WebServiceException {
		if(MyTools.isEmpty(user.getEMail()) || MyTools.isEmpty(user.getLoginName()) || MyTools.isEmpty(user.getPassword())){
			throw new EmptyInputException(user.print());
		}
		return this.uManager.createUser(user);
	}

	public boolean addToFriendList(User user, User userToAdd) throws EmptyInputException, NoUserException, InterruptedException, ExecutionException, JSONException {
		if(MyTools.isEmpty(user.getLoginName())){
			throw new EmptyInputException(user.print());
		}
		return this.uManager.addToFriendList(user, userToAdd);
	}

	public User login(User user) throws EmptyInputException, InterruptedException, ExecutionException, JSONException, WebServiceException {
		if(MyTools.isEmpty(user.getLoginName()) || MyTools.isEmpty(user.getPassword())){
			throw new EmptyInputException("Loginname: " + user.getLoginName() + "; Password: " + user.getPassword());
		}
		return this.uManager.login(user);
	}

	public List<User> friendList(User user) throws InterruptedException, ExecutionException, JSONException, NoUserException{
		return this.uManager.friendList(user);
	}
	
}
